#include "example_interfaces/srv/add_two_ints.hpp"
#include "rclcpp/rclcpp.hpp"

class Client : public rclcpp::Node
{
  private:
	rclcpp::Client<example_interfaces::srv::AddTwoInts>::SharedPtr client;
	void callback(rclcpp::Client<example_interfaces::srv::AddTwoInts>::SharedFuture res)
	{
		auto resp = res.get();
		RCLCPP_INFO(this->get_logger(), "result: %ld", resp->sum);
	}

  public:
	Client(std::string name) : Node(name)
	{
		RCLCPP_INFO(this->get_logger(), "Node %s started", name.c_str());
		client = this->create_client<example_interfaces::srv::AddTwoInts>("addservice");
	}
	void send_request(int a, int b)
	{
		RCLCPP_INFO(this->get_logger(), "send request");
		while (!client->wait_for_service(std::chrono::seconds(1)))
		{
			if (!rclcpp::ok())
			{
				RCLCPP_INFO(this->get_logger(), "rclcpp not ok");
				return;
			}
			RCLCPP_INFO(this->get_logger(), "waiting for service online");
		}
		auto req = std::make_shared<example_interfaces::srv::AddTwoInts_Request>();
		req->a = a;
		req->b = b;
		client->async_send_request(req,
								   std::bind(&Client::callback, this, std::placeholders::_1));
	}
};

int main(int argc, char **argv)
{
	rclcpp::init(argc, argv);
	auto node = std::make_shared<Client>("service_client");
	node->send_request(3, 4);
	rclcpp::spin(node);
	rclcpp::shutdown();
	return 0;
}
